package com.team2.outpatient_sys.web.administrator;

import com.team2.outpatient_sys.pojo.Authority;
import com.team2.outpatient_sys.pojo.Result;
import com.team2.outpatient_sys.pojo.Role;
import com.team2.outpatient_sys.service.AdminRoleAuthorityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/ra")
public class AdminRoleAuthorityController {
    @Autowired
    private AdminRoleAuthorityService adminRoleAuthorityService;

    /**
     * 处理查询所有角色的请求的Controller方法
     * @return 响应内容，封装了查询所有角色请求的结果
     */
    @GetMapping("/allRole")
    public Result getAllRole() {
        List<Role> roleList = adminRoleAuthorityService.getAllRole();

        Result result = new Result();
        if (roleList == null) {
            result.setCode(500);
            result.setMsg("查询所有角色信息时出现异常");
        } else {
            result.setCode(200);
            result.setMsg("查询所有角色信息成功");
            result.setData(roleList);
        }

        return result;
    }

    /**
     * 处理查询所有权限的请求的Controller方法
     * @return 响应内容，封装了查询所有权限请求的结果
     */
    @GetMapping("/allAuthority")
    public Result getAllAuthority() {
        List<Authority> authorityList = adminRoleAuthorityService.getAllAuthority();

        Result result = new Result();
        if (authorityList == null) {
            result.setCode(500);
            result.setMsg("查询所有权限信息时出现异常");
        } else {
            result.setCode(200);
            result.setMsg("查询所有权限信息成功");
            result.setData(authorityList);
        }

        return result;
    }

    /**
     * 处理根据角色id查询角色具有的所有权限的请求的Controller方法
     * @param roleId 需要查询的角色的id
     * @return 响应内容，封装了查询指定id角色具有的权限请求的结果
     */
    @GetMapping("/authoritiesOfRole/{roleId}")
    public Result getAuthoritiesByRoleId(@PathVariable Integer roleId) {
        List<Authority> authorityList = adminRoleAuthorityService.getAuthoritiesByRoleId(roleId);

        Result result = new Result();
        if (authorityList == null) {
            result.setCode(500);
            result.setMsg("根据角色id查询具有的权限信息时出现异常");
        } else {
            result.setCode(200);
            result.setMsg("根据角色id查询具有的权限信息成功");
            result.setData(authorityList);
        }

        return result;
    }

    /**
     * 处理根据角色id修改角色权限请求的Controller方法
     * @param roleId 角色的id
     * @param authorityList 角色拥有的权限的集合
     * @return 响应内容，封装了根据角色id修改角色权限请求的处理结果
     */
    @PutMapping("/roleAuthority/{roleId}")
    public Result updateRoleAuthorityByRoleId(@PathVariable Integer roleId, @RequestBody List<Authority> authorityList) {
        boolean isUpdateSuccess = adminRoleAuthorityService.updateRoleAuthorityByRoleId(roleId, authorityList);
        return isUpdateSuccess ? new Result(200, "角色权限修改成功") : new Result(500, "角色权限修改时发生异常");
    }
}
